package snippets.cse524.activeDates.evaluation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class CareerExtractor {
	private static final int CHAIN_LENGTH = 3;

	public static void main(String [] args) throws FileNotFoundException {
		Scanner in = new Scanner(new FileReader(new File(args[0])));

		while (in.hasNextLine()) {
			
			String line = in.nextLine().trim();
			
			if(line.isEmpty()) continue;

			String[] parts = StringUtils.split(line, '\t');
			for (int i = 1; i < parts.length; ++i) {
				String part = parts[i].toLowerCase();
				// check if there's a canonical name for this guy

				part = part.replace("acting", "").replaceAll("\\.", "").trim();
			}

			if (parts.length != CHAIN_LENGTH + 2) {
//				System.err.println("Invalid line: " + line + " of length "
//				 + parts.length + ", ignoring." + in.hasNextLine());
				continue;
			}

			Pattern year = Pattern.compile("([0-9]{4})");
			Matcher yearMatcher = year.matcher(parts[1]);

			int minYear = Integer.MAX_VALUE;
			int maxYear = Integer.MIN_VALUE;

			while (yearMatcher.find()) {
				int thisYear = Integer.parseInt(yearMatcher.group());

				if (thisYear < minYear)
					minYear = thisYear;
				if (thisYear > maxYear)
					maxYear = thisYear;
			}

			if (minYear == Integer.MAX_VALUE)
				minYear = -1;
			if (maxYear == Integer.MIN_VALUE)
				maxYear = -1;
			
			System.out.println(StringUtils.join(new String [] {parts[3], Integer.toString(minYear), Integer.toString(maxYear), parts[0]}, '\t'));
		}
	}
}
